package cz.muni.fi.pa165.musiclibrary.impl.service.auth;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

import cz.muni.fi.pa165.musiclibrary.api.service.auth.AuthException;

/**
 *
 * @author Maros Kucbel
 * @date 2013-01-22T17:53:06+0100
 */
public final class AuthAdvice implements MethodBeforeAdvice {

    private AuthService authService;
    
    public AuthAdvice(AuthService authService) {
        this.authService = authService;
    }
    
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        String methodName = method.getName();
        if (methodName.startsWith("get")) {
            // get methods don't need authorization
            return;
        }
        String userName = (String) args[1];
        if (!this.authService.isAuthorized(userName)) {
            throw new AuthException("User not logged in.");
        }
    }

}